package com.daogu.fuelcard.service.impl;

import com.daogu.fuelcard.dao.TransactionRecordMapper;
import com.daogu.fuelcard.entity.OilCard;
import com.daogu.fuelcard.entity.TransactionRecord;
import com.daogu.fuelcard.service.TransactionRecordService;
import com.daogu.fuelcard.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class TransactionRecordServiceImpl implements TransactionRecordService {

    @Autowired
    private TransactionRecordMapper transactionRecordMapper;

    @Override
    public Result<Void> insert(TransactionRecord transactionRecord) {
        TransactionRecord checkTransaction = transactionRecordMapper.
                selectByCardNoAndTradingTime(transactionRecord.getCardNo(), transactionRecord.getTradingTime());
        if (checkTransaction!=null) {
            return Result.error("交易记录已存在");
        }
        transactionRecordMapper.insert(transactionRecord);
        return Result.accept();
    }

    @Override
    public Result<Void> deletedById(Integer id) {
        transactionRecordMapper.deletedById(id);
        return Result.accept();
    }

    @Override
    public Result<Void> updateCheckStatus(Integer id,Integer checkStatus) {
        transactionRecordMapper.updateCheckStatus(Collections.singletonList(id),checkStatus);
        return Result.accept();
    }

    @Override
    public Result<PageInfo<TransactionRecord>> getTransactionRecord(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TransactionRecord> transactionRecords = transactionRecordMapper.selectAll();
        PageInfo<TransactionRecord> pageInfo = new PageInfo<>(transactionRecords);
        return Result.accept(pageInfo);
    }
}
